﻿using System.Windows;
using System.Windows.Input;
using Sentio.CodeReview.Foundation;
using System;
using System.Threading;

namespace Sentio.CodeReview.Editor
{
    /// <summary>
    /// Interaction logic for AddCodeComment.xaml
    /// </summary>
    public partial class AddCodeComment : Window
    {
        private readonly string _filePath;
        private readonly SolutionConfiguration _svnConfig;
        private readonly int _from, _to;
        private BlockCommentStatus _status = BlockCommentStatus.Neutral;

        public AddCodeComment(int from, int to, string filePath)
        {
            InitializeComponent();

            _svnConfig = new SolutionConfiguration()
            {
                SvnProjectRoot = "https://svn-code-review.googlecode.com/svn/trunk/Sentio.CodeReview",
                Username = "karim.dev",
                SolutionBaseDirectory = @"C:\Users\Filip\Documents\Sentio.CodeReview\"
            };

            From.Text = from.ToString();
            To.Text = to.ToString();
            _filePath = filePath;
            _from = from;
            _to = to;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string commentBody = CommentBody.Text;
            ThreadPool.QueueUserWorkItem(delegate
            {
                SvnTargetRepository repo = new SvnTargetRepository(_svnConfig);
                repo.AddComment(_filePath.Replace(_svnConfig.SolutionBaseDirectory, ""),
                    new BlockComment
                    {
                        Author = _svnConfig.Username,
                        CommentBody = commentBody,
                        CreatedAt = DateTime.Now,
                        FromOffset = _from,
                        ToOffset = _to,
                        Status = _status
                    });
            });
            Close();
        }

        private void Positive_Checked(object sender, RoutedEventArgs e)
        {
            _status = BlockCommentStatus.Positive;
        }

        private void Negative_Checked(object sender, RoutedEventArgs e)
        {
            _status = BlockCommentStatus.Negative;
        }

        private void Neutral_Checked(object sender, RoutedEventArgs e)
        {
            _status = BlockCommentStatus.Neutral;
        }
    }
}
